package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.EducationResource;
import com.tju.ocean_ecology_website.dao.EducationResourceDao;
import com.tju.ocean_ecology_website.service.EducationResourceService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 教育资源表(EducationResource)表服务实现类
 *
 * @author shaolong
 * @since 2025-04-29 19:31:59
 */
@Service("educationResourceService")
public class EducationResourceServiceImpl implements EducationResourceService {
    @Resource
    private EducationResourceDao educationResourceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public EducationResource queryById(Long id) {
        return this.educationResourceDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param educationResource 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<EducationResource> queryByPage(EducationResource educationResource, PageRequest pageRequest) {
        long total = this.educationResourceDao.count(educationResource);
        return new PageImpl<>(this.educationResourceDao.queryAllByLimit(educationResource, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param educationResource 实例对象
     * @return 实例对象
     */
    @Override
    public EducationResource insert(EducationResource educationResource) {
        this.educationResourceDao.insert(educationResource);
        return educationResource;
    }

    /**
     * 修改数据
     *
     * @param educationResource 实例对象
     * @return 实例对象
     */
    @Override
    public EducationResource update(EducationResource educationResource) {
        this.educationResourceDao.update(educationResource);
        return this.queryById(educationResource.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.educationResourceDao.deleteById(id) > 0;
    }
}
